using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Logs.Core;

namespace Pixysoft.Framework.Logs.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "Document")]
    class UnitTest_012_Document
    {
        private static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {
            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("Serialize")]
        public static void TestCase_000_InitializeDocument()
        {
            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = "pixysoft";
            string postfix = ".txt";

            Document doc = new Document(path, filename, postfix);
            assert.IsEqual(path, doc.Filepath);
            assert.IsEqual(filename, doc.Filename);
            assert.IsEqual(postfix, doc.Postfix);

            string element = "log";
            string rawdate = StringHelper.GetRawDateTime();
            string text = "helloworld";
            string id = StringHelper.GetId();
            IRecord rec = doc.Records.Add(element);
            rec.Text = text;
            assert.IsEqual(element, rec.ElementName);
            assert.Write(rec.RawDate);
            assert.IsEqual(text, rec.Text);
            assert.Write(rec.Id);
            assert.Write(rec.Date);

            IRecordProperty property = rec.Properties.Add("key", "value");
            assert.IsEqual("key", property.Key);
            assert.IsEqual("value", property.Value);

            IRecord subrec = rec.Records.Add("subrecord");
            assert.IsEqual("subrecord", subrec.ElementName);

            Assert.Cache.Push(doc);
        }

        [TestCase("Serialize")]
        public static void TestCase_001_Serialize()
        {
            Assert.Call(UnitTest_012_Document.TestCase_000_InitializeDocument);

            IDocument doc = Assert.Cache.Pop<IDocument>();

            assert.Write("-----------------------------");

            assert.Write(doc.Serialize());

            assert.Write(doc.Serialize(true));

            foreach (IRecord rec2 in doc.Records)
            {
                foreach (IRecordProperty property2 in rec2.Properties)
                {
                    assert.Write(property2.Key);
                    assert.Write(property2.Value);
                }
            }

            IRecord rec = doc.Records[0];
            IRecordProperty property = rec.Properties[0];

            doc.Records.Sort();
            assert.IsEqual(1, doc.Records.Count);
            doc.Records.Clear();
            assert.IsEqual(0, doc.Records.Count);
            doc.Records.Add(rec);
            assert.IsEqual(1, doc.Records.Count);
            assert.IsTrue(doc.Records.Contains("log"));


            assert.IsEqual(3, rec.Properties.Count);
            rec.Properties.Clear();
            assert.IsEqual(0, rec.Properties.Count);
            rec.Properties.Add(property.Key, property.Value);
            assert.IsEqual(1, rec.Properties.Count);
            assert.IsTrue(rec.Properties.Contains("id"));
            assert.Write(rec.Properties["id"]);
        }

    }
}
